/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.stockkeeper.stock.entities;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import org.letmeshare.stockkeeper.validator.ValidationException;
import org.letmeshare.stockkeeper.validator.Validator;

/**
 *
 * @author ibisuser1
 */
@Entity
public class Item implements Serializable, Validator {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;
    private String name;
    private String code;
    private double unitPrice;
    private double exclusiveTax;//if any
    @ManyToOne
    private Category category;
    private boolean obsolete;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public double getUnitPrice() {
        return unitPrice;
    }

    public void setUnitPrice(double unitPrice) {
        this.unitPrice = unitPrice;
    }

    public double getExclusiveTax() {
        return exclusiveTax;
    }

    public void setExclusiveTax(double exclusiveTax) {
        this.exclusiveTax = exclusiveTax;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public boolean isObsolete() {
        return obsolete;
    }

    public void setObsolete(boolean obsolete) {
        this.obsolete = obsolete;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Item)) {
            return false;
        }
        Item other = (Item) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.letmeshare.stockkeeper.entities.stock.Item[ id=" + id + " ]";
    }

    public void validate() throws ValidationException {
        if (unitPrice < 0) {
            throw new ValidationException("Invalid unit price");
        }
        if (exclusiveTax < 0) {
            throw new ValidationException("Invalid tax");
        }
        if (name == null || name.isEmpty()) {
            throw new ValidationException("Name required");
        }
        if (code == null || code.isEmpty()) {
            throw new ValidationException("Code required");
        }
    }
}
